var is_online = true
var baseURL = is_online ? 'https://agreement.ameimeika.com/' : 'http://signe.ameimeika.com/';
/**
** 封装请求方法
** url 接口请求地址
** data 接口请求参数（无需请求方式参数，则此项可以为空，否则必须传）
** params 请求方式参数（可以为空）
**/

//创建节点
var wrap = document.createElement("div");
var loadingbox = document.createElement("div");
var box = document.createElement("div");
var child1 = document.createElement("div");
var child2 = document.createElement("div");
//设置节点id
wrap.id = 'loading';
box.classList.add("mui-loadingbox");
child1.classList.add("mui-spinner");
//设置节点属性
wrap.style.width = '100%';
wrap.style.height = '100%';
wrap.style.position = 'fixed';
wrap.style.top = '0';
wrap.style.right = '0';
wrap.style.bottom = '0';
wrap.style.left = '0';
wrap.style.background = 'rgba(0,0,0,.4)';
wrap.style.zIndex = "99";
wrap.style.display = "none";

loadingbox.style.position = 'absolute';
loadingbox.style.top = '0';
loadingbox.style.left = '0';
loadingbox.style.width = '100%';
loadingbox.style.height = '100%';
loadingbox.style.display = "flex";
loadingbox.style.alignItems = "center";
loadingbox.style.justifyContent = "center";

box.style.width = 'auto';
box.style.maxWidth = '50%';
box.style.display = "flex";
box.style.alignItems = "center";
box.style.background = "rgba(37,38,45,0.9)";
box.style.padding = "13px 16px";
box.style.boxSizing = "border-box";
box.style.borderRadius = "6px";
box.style.transition = "all .4s";

child2.style.color = "#ccc"
child2.style.fontSize = "14px"
child2.style.marginLeft = "2px"
child2.innerHTML = "玩命加载中..."
// 插入节点
box.appendChild(child1);
box.appendChild(child2);
loadingbox.appendChild(box)
wrap.appendChild(loadingbox)
document.body.appendChild(wrap);

(function ($) {
  $.hide = function () {
    $("#loading").hide()
  }
  $.show = function () {
    $("#loading").show()
  }
})(jQuery);

function $request(url, data, params) {
  $.show();
  return new Promise(function (resolve, reject) {
    $.ajax({
      url: baseURL + url,
      type: params && params.type || 'POST',
      dataType: params && params.dataType || 'JSON',
      data: data,
      success: function (res) {
        $.hide();
        if (res.code != 200) {
          mui.toast(res.msg)
        } else {
          resolve(res)
        }
      },
      error: function (res) {
        $.hide();
        reject(res)
      },
      headers: {
        "x-access-token": localStorage.getItem('token') || '',
      }
    });
  })
}

function get(url, data, ) {
  return $request(url, data, { type: 'GET' })
}
function post(url, data, ) {
  return $request(url, data)
}
function put(url, data, ) {
  return $request(url, data, { type: 'PUT' })
}